﻿@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>研发管理</title>
    <link href="~/Scripts/jquery-easyui/themes/bootstrap/easyui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-easyui/jquery.min.js"></script>
    <script src="~/Scripts/jquery-easyui/jquery.easyui.min.js"></script>
    <script src="~/Scripts/jquery-easyui/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/Content/themes/icon.css" rel="stylesheet" />
    <script src="~/Content/js/common.js"></script>
    <link href="~/Content/themes/table.css" rel="stylesheet" />
    <link href="~/Content/formstyles.css" rel="stylesheet" />
    <script type="text/javascript">
        var viewdata = {
            view: 'RDGrid',
            keyCode: 'RDIndex',
        }
        var lock = false;
        var columns = [[
                                     {
                                         field: 'Status', title: '状态', sortable: false, width: 45,
                                         formatter: function (value, row, index) {
                                             var x;
                                             switch (value) {
                                                 case 0:
                                                     x = "未完成";
                                                     break;
                                                 case 1:
                                                     x = "已完成";
                                                     break;
                                                 default:
                                                     x = value;
                                                     break;
                                             }
                                             return x;
                                             //return value == 1 ? '<img src="../../Content/themes/icon/chk_checked.gif" alt="已确认" title="项目已确认" />' : '<img src="../../Content/themes/icon/chk_unchecked.gif" alt="未确认" title="项目未确认" />';
                                         }
                                     },
                                 { field: 'Department', title: '部门', sortable: false, hidden: false },
                                {
                                    field: 'ProjectClerk', title: '设备负责人', sortable: false,   hidden: false
                                }, { field: 'FName', title: '设备名称', sortable: false, hidden: false },
                                
                                {
                                    field: 'ProjectManager', title: '项目经理', sortable: false,
                                    //width: 60
                                },
                                 {
                                     field: 'RDType', title: '研发类别', sortable: false,
                                 },
                                  {
                                      field: 'pLevel', title: '优先级', sortable: false,
                                  },
                                   {
                                       field: 'aFactor', title: '先进系数', sortable: false,
                                   },
                                    {
                                        field: 'iFactor', title: '增型系数', sortable: false,
                                    },
                                     {
                                         field: 'RDEstCost', title: '研发费用估算', sortable: false,
                                     },
                                      {
                                          field: 'estPrice', title: '预售单价', sortable: false,
                                      }, {
                                          field: 'startTime', title: '开始时间', sortable: false, formatter: getDate
                                      }, {
                                          field: 'endTime', title: '结束时间', sortable: false, formatter: getDate
                                      }, {
                                          field: 'yylxTime', title: '预研立项时间', sortable: false, formatter: getDate
                                      }, {
                                          field: 'cbsjTime', title: '初步设计时间', sortable: false, formatter: getDate
                                      }, {
                                          field: 'gcsjTime', title: '工程设计时间', sortable: false, formatter: getDate
                                      }, {
                                          field: 'yjzzTime', title: '样机制作时间', sortable: false, formatter: getDate
                                      }, {
                                          field: 'sxcgTime', title: '首销成功时间', sortable: false, formatter: getDate
                                      },  
                                      //{
                                      //    field: 'awardAmount', title: '奖励金额', sortable: false,
                                      //}, {
                                      //    field: 'evalAmount', title: '考核金额', sortable: false,
                                      //},
                                  { field: 'UpdateBy', title: '更新人', width: 60 },
                                  { field: 'UpdateTime', title: '更新时间', sortable: true, width: 70, formatter: getDate },
                                  {
                                      field: 'Remark', title: '项目描述', width: 500,hidden: true,
                                      formatter: function (value, row, index) {
                                          return value.length > 20 ? '<span title="' + value + '">' + value + '</span>' : value;
                                      }
                                  },
                                  { field: 'CreateBy', title: '创建人', width: 60, hidden: true },
                                  { field: 'CreateTime', title: '创建时间', sortable: true, width: 70, formatter: getDate, hidden: true },

        ]];
        //主表自动加载函数
        $(function () {          
            $.ajax({     //请求当前用户可以操作的按钮
                url: "/Button/GetUserAuthorizeButton?r=" + Math.random(),
                type: "post",
                data: { "KeyCode": "RDIndex", "KeyName": "RDIndex" },
                dataType: "json",
                timeout: 5000,
                success: function (data) {
                    if (data.success) {
                        //前端添加按钮
                        //data.toolbar.push({ "text": "导出", "id": "export", "iconCls": "icon-link", "handler": "linkOper();" })
                        //data.toolbar.push({ "text": "成本管理", "id": "btCostMgr", "iconCls": "icon-link", "handler": "costMgr();" })
                        var toolbar = getToolBar(data);      //common.js
                        if (data.search) {     //判断是否有浏览权限 
                            $("#ui_main_dg").datagrid({       //初始化datagrid
                                url: "/RD/GetRDList?&r=" + Math.random(),
                                striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
                                queryParams: {
                                    "view": viewdata.view,
                                },
                                idField: 'RDID',
                                sort: 'UpdateTime',
                                order: 'desc',
                                pageList: [20, 40, 60, 80, 100],
                                frozenColumns: [[
                                               {
                                                   hidden: true,
                                                   title: 'ID',
                                                   field: 'ProjectID'
                                               },
                                                 {
                                                     width: '100',
                                                     title: '项目编码',
                                                     field: 'ProjectNo',
                                                     formatter: function (value, row, index) {
                                                         return value.length > 8 ? '<span title="' + value + '">' + value + '</span>' : value;
                                                     }
                                                 },
                                                 { //width: '120',
                                                     title: '项目名称',
                                                     field: 'ProjectName',
                                                     formatter: function (value, row, index) {
                                                         return value.length > 8 ? '<span title="' + value + '">' + value + '</span>' : value;
                                                     }
                                                 }

                                ]],
                                columns: columns,
                                toolbar: toolbar.length == 0 ? null : toolbar,
                                onSelect: function (rowIndex, rowData) {
                                                        // menuControl();
                                },
                                onLoadSuccess: function (data) {
                        

                                }
                            });
                        }
                        else {
                            $.show_alert("提示", "无权限，请联系管理员！");
                        }
                    } else {
                        $.show_alert("错误", data.result);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus == "timeout") {
                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                    }
                    else {
                        $.show_alert("错误", textStatus + "：" + errorThrown);
                    }
                }
            })
            //回车搜索
            $("#ui_main_search").find('input').on('keyup', function (event) {
                if (event.keyCode == '13') {
                    ui_main_searchdata();
                }
            })
        });
        //菜单标灰处理函数
        function menuControl() {
            var rows = $("#ui_main_dg").datagrid("getChecked");        
            switch(rows[0].status) {
                case "":                   
                case 0:
                    $("#delete").linkbutton("enable");//允许删除
                    $("#confirm").linkbutton("enable");
                    $("#unconfirm").linkbutton("disable");
                    $("#assign").linkbutton("disable");
                    $("#unassign").linkbutton("disable");
                    break;
                case 1:
                    $("#delete").linkbutton("disable");//禁止删除
                    $("#confirm").linkbutton("disable");
                    $("#unconfirm").linkbutton("enable");
                    $("#assign").linkbutton("enable");
                    $("#unassign").linkbutton("disable");
                    break;
                case 2:
                    $("#delete").linkbutton("disable");//禁止删除
                    $("#confirm").linkbutton("disable");
                    $("#unconfirm").linkbutton("disable");
                    $("#assign").linkbutton("disable");
                    $("#unassign").linkbutton("enable");
                    break;
                case 3:
                    $("#delete").linkbutton("disable");//禁止删除
                    break;
                default:
                    $("#delete").linkbutton("disable");//禁止删除 
                    break;
            }

        }
        //显示/隐藏 toolbar按钮 
        function easyRightDisplay() {
            //获取所有的toolbar按钮
            var button = $('div.datagrid div.datagrid-toolbar a');
            for (var i = 0; i < button.length; i++) {
                var toolbar = button[i];
                var id = toolbar.id;
                if (id == "add") {  //隐藏Id为add的按钮
                    $('div.datagrid div.datagrid-toolbar a').eq(i).hide();
                }
                if (id == "delete") {  //不隐藏id为delete的按钮
                    //button.eq(i).hide();
                }
                //如果按钮都没权限，隐藏了可直接隐藏toolbar
                //$('div.datagrid div.datagrid-toolbar').hide();
            }
        }


 //新增项目
  function AddRDIndex() {
            $("<div/>").dialog({
                 id: "ui_main_add_dialog",
                 href: "/RD/RDEdit",
                 title: "新增项目",
                 height: $(window).height(),
                 width: $(window).width(),
                 modal:true,
                 toolbar: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function () {                      
                        if ($("#RDMainForm").form('validate')) {//表单校验
                            if (lock) { return; } else { lock = true; }
                        $.ajax({     //提交数据保存
                            url: "/RD/AddNewProject?r=" + Math.random(),
                            type: "post",
                            data: {
                                "KeyCode": "CreateRDBill",
                                "ProjectID": $("#ProjectID").val(),
                                "ProjectNo": $("#ProjectNo").val(),
                                "ProjectName": $("#ProjectName").val(),
                                "ProjectManager": $("#ProjectManager").val(),
                                "ProjectClerk": $("#ProjectClerk").val(),
                                //"Remark": $("#Remark").val(),
                                "Department": $("#Department").val(),
                                "RDType": $("#RDType").val(),
                                "RDEstCost": $("#RDEstCost").val(),
                                "estPrice": $("#estPrice").val(),
                                "pLevel": $("#pLevel").val(),
                                "aFactor": $("#aFactor").val(),
                                "iFactor": $("#iFactor").val(),
                                "startTime": $('#startTime').datebox('getValue'),
                                "endTime": $('#endTime').datebox('getValue'),
                                "FName": $("#FName").val(),
                            },
                            dataType: "json",
                            timeout: 5000,
                            success: function (data) {
                                lock = false;
                                if (data.success) {
                                    $.show_alert("成功", "保存成功！");
                                    $("#ui_main_add_dialog").dialog('destroy'); 
                                    $("#ui_main_dg").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
                                } else {
                                    $.show_alert("提示", data.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                if (textStatus == "timeout") {
                                    $.show_alert("提示", "请求超时，请刷新当前页重试！");
                                }
                                else {
                                    $.show_alert("错误", textStatus + "：" + errorThrown);
                                }
                            }
                        })
                        } else {
                            $.show_alert('操作提示', '存在校验项未通过！', "warning"); 
                        }
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-cross',
                    handler: function () { $("#ui_main_add_dialog").dialog('destroy'); }
                }],
                onLoad: function () {
                    //$("#ProjectNo").textbox('enable');
                    //$("#ProjectName").textbox('enable');
                    //$("#ProjectManager").textbox('enable');
                    //$("#ProjectClerk").textbox('enable');
                    //$("#Remark").textbox('enable');
                    //$("#CreateBy").attr('hidden', 'hidden');
                    //$("#CreateTime").attr('hidden', 'hidden');
                    //$("#UpdateTime").attr('hidden', 'hidden');
                    //$("#UpdateBy").attr('hidden', 'hidden');
                    $("#tt").attr('hidden', 'hidden');//隐藏节点管理
                 
                },
                onClose: function () {
                    $("#ui_main_add_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

        
  //项目管理角色授权：用于成本查看权限的管控
    function RoleAuthorize() {     
            $("<div/>").dialog({
                id: "ui_main_dg_role_authorize_dialog",
                href: "/Project/ProjectRoleAuthorize",
                title: "角色授权：",
                height: 280,
                width: 420,
                modal: true,
                buttons: [{
                    id: "ui_main_setrole_btn",
                    text: '确 定',
                    handler: function () {
                        $("#SetRDRoleForm").form("submit", {
                            url: "/RDCost/SetRoleField",
                            onSubmit: function (param) {
                                $('#ui_main_setrole_btn').linkbutton('disable');
                                param.fieldName = $("#fieldName").combobox('getValue');
                                param.RoleIDs = $("#comboxrole").combobox('getValues');
                            },
                            success: function (data) {
                                var dataJson = eval('(' + data + ')');
                                if (dataJson.success) {
                                    $("#ui_main_dg_role_authorize_dialog").dialog('destroy');
                                    $.show_alert("提示", dataJson.msg);
                                   // $("#ui_user_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                } else {
                                    $('#ui_main_setrole_btn').linkbutton('enable');
                                    $.show_alert("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                }],
                onLoad: function () {
                   
                },
                onClose: function () {
                    $("#ui_main_dg_role_authorize_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }


        //从前端导出,只能用模拟表单
        // 直接通过ajax请求文件数据，jquery下载文件时不能触发浏览器弹出保存文件对话框！ 可以在javascript中访问下载的文件数据   
        // 模拟表单提交同步方式下载文件
        // 能够弹出保存文件对话框
        function RDIndexExport() {
            //console.log("ajaxDownloadSynchronized");
            var rows = $("#ui_main_dg").datagrid("getRows");
            for (var i = 0; i < rows.length; i++) {    //进行数据处理
                switch (rows[i].status) {
                    case 0:
                        rows[i].status = "待确认";
                        break;
                    case 1:
                        rows[i].status = "已确认";
                        break;
                    case 2:
                        rows[i].status = "已下达";
                        break;
                    case 3:
                        rows[i].status = "已结项";
                        break;
                    case 5:
                        rows[i].status = "已终止";
                        break;
                    default:                         
                        break;
                }
            //移除不要的字段
                delete rows[i].ProjectID;
            }
            var bodyData = JSON.stringify(rows);  //转成json字符串
            //替换中文标题
            var a = bodyData.replace(/ProjectNo/g, "项目编码").replace(/ProjectName/g, "项目名称").replace(/status/g, "状态")
            .replace(/ProjectClerk/g, "业务员").replace(/ProjectManager/g, "管理员").replace(/EstWorkTime/g, "预估工时").replace(/UpdateBy/g, '最后更新人')
            .replace(/UpdateTime/g, "最后更新时间").replace(/Remark/g, "项目描述").replace(/CreateBy/g, "创建人").replace(/CreateTime/g, "创建时间");
            //var postData = {
            //    data: bodyData
            //};
         
            var url = "/Project/ExportExcel";
            var fileName = "testAjaxDownload.xls";
            var form = $("<form></form>").attr("action", url).attr("method", "post");
            form.append($("<input></input>").attr("type", "hidden").attr("name", "bodyData").attr("value", a));
            form.appendTo('body').submit().remove();            
        }

        //从前端导出
        function exportRDIndex2() {
            var rows = $("#ui_main_dg").datagrid("getRows");
             
            var bodyData = JSON.stringify(rows);  //转成json字符串

            //替换中文标题
           // var a = bodyData.replace(/SaleOrderNo/g, "订单编号").replace(/OrderType/g, "订单类型").replace(/FromWhere/g, "订单来源")
           //.replace(/OrganizedId/g, "机构").replace(/SaleUser/g, "销售员").replace(/SaleTc/g, "销售提成").replace(/OrderDate/g, '订单日期')
           //.replace(/ContractNo/g, "合同编号").replace(/Amount/g, "总额").replace(/Advance/g, "首付款").replace(/AdvanceDate/g, "首付款日期")
           //.replace(/PayMethod/g, "支付方式").replace(/Retainage/g, "尾款").replace(/RetainageDate/g, "尾款日期").replace(/InlayPrice/g, "镶嵌款")
           //.replace(/CustManager/g, "客户经理").replace(/EquityNo/g, "认股书编号").replace(/LogisticsTotal/g, "物流费用")
           //.replace(/Remarks/g, "备注").replace(/CompletedStatus/g, "状态").replace(/CustomerId/g, "终端客户");

            var postData = {
                data: bodyData
            };


        }
 
        //删除项目
        function DelRDIndex() {
            var rows = $("#ui_main_dg").datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要删除的项目");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量删除");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $.messager.confirm('提示', '确定删除：' + rows[0].ProjectName + '？', function (r) {
                if (r) {
                    $.ajax({
                        url: "/RD/DelProjectByID?r=" + Math.random(),
                        data: {
                            ID: rows[0].ProjectID
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_main_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                ////如果“用户管理”标签页处于打开状态需要刷新，否则被删除的角色还显示，再点编辑用户就出错
                                //if ($('#tabs').tabs('exists', '用户管理')) {
                                //    $('#tabs').tabs('getTab', '用户管理').panel('refresh');
                                //}

                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }

        //项目状态：确认
        function confirmRDIndex() {
            var rows = $("#ui_main_dg").datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要确认的项目");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量处理");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $.messager.confirm('提示', '确认该项目：' + rows[0].ProjectName + '？', function (r) {
                if (r) {
                    $.ajax({
                        url: "/Project/confirmProjectByID?r=" + Math.random(),
                        data: {
                            ID: rows[0].ProjectID,
                            confirm: "confirm"
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_main_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                ////如果“用户管理”标签页处于打开状态需要刷新，否则被删除的角色还显示，再点编辑用户就出错
                                //if ($('#tabs').tabs('exists', '用户管理')) {
                                //    $('#tabs').tabs('getTab', '用户管理').panel('refresh');
                                //}

                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }
        //取消确认项目
        function unconfirmRDIndex() {
            var rows = $("#ui_main_dg").datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要取消确认的项目");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量处理");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $.messager.confirm('提示', '取消确认该项目：' + rows[0].ProjectName + '？', function (r) {
                if (r) {
                    $.ajax({
                        url: "/Project/confirmProjectByID?r=" + Math.random(),
                        data: {
                            ID: rows[0].ProjectID,
                            confirm: "unconfirm"
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_main_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                ////如果“用户管理”标签页处于打开状态需要刷新，否则被删除的角色还显示，再点编辑用户就出错
                                //if ($('#tabs').tabs('exists', '用户管理')) {
                                //    $('#tabs').tabs('getTab', '用户管理').panel('refresh');
                                //}

                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }      
        function ui_main_searchdata() {
            $("#ui_main_dg").datagrid('load', {
                ProjectNo: $('#txtProjectNo').val(),
                ProjectName: $('#txtRDName').val(),
                status: $('#FStatus').val(),
                CreateTimeS: $('#txtAddBeginDate').datetimebox('getValue'),
                CreateTimeE: $('#txtAddEndDate').datetimebox('getValue')
            });
            // $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
        }

        function ui_main_cleardata() {
            $('#ui_main_search input').val('');
            $('#ui_main_search select').val('select');
            $('#txtAddBeginDate').datetimebox('setValue', '');
            $('#txtAddEndDate').datetimebox('setValue', '');
            $("#ui_main_dg").datagrid('load', {});//清空条件自动加载数据

            //  $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
        }

        function uploadfile(appendlistid, appenddatagrid) {
            $("<div/>").dialog({
                id: "ui_main_upload_dialog",
                href: "/Project/uploadform",
                title: "上传附件",
                height: 270,
                width: 510,
                modal: true,
                buttons: [{
                    id: "ui_file_add_btn",
                    text: '添 加',
                    handler: function () {
                        //获取上传文件控件内容
                        var file = $("#fileImport").filebox("files")[0];
                        //判断控件中是否存在文件内容，如果不存在，弹出提示信息，阻止进一步操作
                        if (file == null) { alert('错误，请选择文件'); return; }
                        //获取文件名称
                        var fileName = file.name;
                        //获取文件类型名称
                        var file_typename = fileName.substring(fileName.lastIndexOf('.'), fileName.length);
                        //这里限定上传文件文件类型必须为.xlsx，如果文件类型不符，提示错误信息
                        if (file_typename == '.xlsx' || true) {
                            //计算文件大小
                            var fileSize = 0;
                            //如果文件大小大于1024字节X1024字节，则显示文件大小单位为MB，否则为KB
                            if (file.size > 1024 * 1024) {
                                fileSize = Math.round(file.size * 100 / (1024 * 1024)) / 100;
                                if (fileSize > 10) {
                                    alert('错误，文件超过10MB，禁止上传！'); return;
                                }
                                fileSize = fileSize.toString() + 'MB';
                            }
                            else {
                                fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
                            }
                            //将文件名和文件大小显示在前端label文本中
                            document.getElementById('fileName').innerHTML = "<span style='color:Blue'>文件名: " + file.name + ',大小: ' + fileSize + "</span>";
                            //获取form数据
                            var formData = new FormData($("#importFileForm")[0]);
                            //调用apicontroller后台action方法，将form数据传递给后台处理。contentType必须设置为false,否则chrome和firefox不兼容
                            $.ajax({
                                url: "/Project/uploadfile?appendlistid=" + appendlistid + "&r=" + Math.random(),
                                type: 'POST',
                                data: formData,
                                async: false,
                                cache: false,
                                contentType: false,
                                processData: false,
                                success: function (returnInfo) {
                                    //上传成功后将控件内容清空，并显示上传成功信息
                                    document.getElementById('fileImport').value = null;
                                    document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";
                                    appenddatagrid.datagrid("reload");//刷新附件清单
                                },
                                error: function (returnInfo) {
                                    //上传失败时显示上传失败信息
                                    document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";
                                }
                            });
                        }
                        else {
                            alert("文件类型错误");
                            //将错误信息显示在前端label文本中
                            document.getElementById('fileName').innerHTML = "<span style='color:Red'>错误提示:上传文件应该是.xlsx后缀而不应该是" + file_typename + ",请重新选择文件</span>"
                        }
                    }
                }, {
                    text: '关 闭',
                    handler: function () {
                        $("#ui_main_upload_dialog").dialog('destroy');

                    }
                }],
                onLoad: function () {

                },
                onClose: function () {
                    $("#ui_main_upload_dialog").dialog('destroy');  //销毁dialog对象                    
                }
            });
        }
        //根据附件id删除附件，更新表格
        function Delfile(appenddatagrid) {
            var rows = appenddatagrid.datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要删除的项目");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量删除");
                appenddatagrid.datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $.messager.confirm('提示', '确定删除：' + rows[0].AppendName + '？', function (r) {
                if (r) {
                    $.ajax({
                        url: "/Common/DelAppendByID?r=" + Math.random(),
                        data: {
                            ID: rows[0].AppendID,
                            AppendPath: rows[0].AppendPath
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_main_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                appenddatagrid.datagrid("reload");//刷新附件清单
                                ////如果“用户管理”标签页处于打开状态需要刷新，否则被删除的角色还显示，再点编辑用户就出错
                                //if ($('#tabs').tabs('exists', '用户管理')) {
                                //    $('#tabs').tabs('getTab', '用户管理').panel('refresh');
                                //}

                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }
        function downloadfile(appenddatagrid) {
            var rows = appenddatagrid.datagrid("getChecked");
            if (rows.length < 1) {
                $.show_alert("提示", "请先勾选要下载的项目");
                return;
            }
            if (rows.length > 1) {
                $.show_alert("提示", "不支持批量处理");
                appenddatagrid.datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            // var path = "${ctx}/file/download.do?filePath=" + value ;  
            window.open("../uploads/" + rows[0].AppendPath);
        }


        //修改项目节点--节点维护
        function EditbillRDIndex() {
            var row = $("#ui_main_dg").datagrid("getSelected");
            if (row==null ||row.length < 1) {
                $.show_alert("提示", "请先勾选要更新的项目");
                return;
            }
            if (row.length > 1) {
                $.show_alert("提示", "不支持批量修改项目");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $("<div/>").dialog({
                id: "ui_main_edit_dialog",
                href: "/RD/RDEdit",
                title: "更新项目",
                height: $(window).height(),
                width: $(window).width(),
                modal: true,
                toolbar: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function () {
                        if(lock){return;}else{lock=true;}
                        if ($("#RDMainForm").form('validate')) {//表单校验
                            $.ajax({     //提交数据保存
                                url: "/RD/updateRDMain?r=" + Math.random(),
                                type: "post",
                                data: {
                                    "KeyCode": "EditRDBill",
                                    "ProjectID": $("#ProjectID").val(),
                                    "ProjectNo": $("#ProjectNo").val(),
                                    "ProjectName": $("#ProjectName").val(),
                                    "ProjectManager": $("#ProjectManager").val(),
                                    "ProjectClerk": $("#ProjectClerk").val(),
                                    //"Remark": $("#Remark").val(),
                                    "Department": $("#Department").val(),
                                    "RDType": $("#RDType").val(),
                                    "RDEstCost": $("#RDEstCost").val(),
                                    "estPrice": $("#estPrice").val(),
                                    "pLevel": $("#pLevel").val(),
                                    "aFactor": $("#aFactor").val(),
                                    "iFactor": $("#iFactor").val(),
                                    "startTime": $('#startTime').datebox('getValue'),
                                    "endTime": $('#endTime').datebox('getValue'),
                                    "FName": $("#FName").val(),

                                },
                                dataType: "json",
                                timeout: 5000,
                                success: function (data) {
                                    if (data.success) {
                                        $.show_alert("成功", "保存项目主表数据成功！");
                                        //$("#ui_main_edit_dialog").dialog('destroy');
                                        //$("#ui_main_dg").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
                                    } else {
                                        $.show_alert("提示", data.msg);
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    if (textStatus == "timeout") {
                                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                                    }
                                    else {
                                        $.show_alert("错误", textStatus + "：" + errorThrown);
                                    }
                                }
                            });
                        } else {
                            $.show_alert("错误", "表单校验未通过！");
                        }

                        $.ajax({     //提交节点数据保存
                            url: "/RD/updateRDNode?r=" + Math.random(),
                            type: "post",
                            data: {
                                "ProjectID": $("#ProjectID").val(),
                                "bsPSTime": $("#bsPSTime").datebox('getValue'),
                                "bsPETime": $("#bsPETime").datebox('getValue'),                               
                                "bsRemark": $("#bsRemark").val(),
                                "bsExecutor": $("#bsExecutor").val(),
                                "bsComplete": $("#bsComplete").prop("checked"),

                                "tyPSTime": $("#tyPSTime").datebox('getValue'),
                                "tyPETime": $("#tyPETime").datebox('getValue'),                               
                                "tyRemark": $("#tyRemark").val(),
                                "tyExecutor": $("#tyExecutor").val(),
                                "tyComplete": $("#tyComplete").prop("checked"),

                                "dnPSTime": $("#dnPSTime").datebox('getValue'),
                                "dnPETime": $("#dnPETime").datebox('getValue'),                              
                                "dnRemark": $("#dnRemark").val(),
                                "dnExecutor": $("#dnExecutor").val(),
                                "dnComplete": $("#dnComplete").prop("checked"),

                                "mePSTime": $("#mePSTime").datebox('getValue'),
                                "mePETime": $("#mePETime").datebox('getValue'),                              
                                "meRemark": $("#meRemark").val(),
                                "meExecutor": $("#meExecutor").val(),
                                "meComplete": $("#meComplete").prop("checked"),
       
                                "cnPSTime": $("#cnPSTime").datebox('getValue'),
                                "cnPETime": $("#cnPETime").datebox('getValue'),                              
                                "cnRemark": $("#cnRemark").val(),
                                "cnExecutor": $("#cnExecutor").val(),
                                "cnComplete": $("#cnComplete").prop("checked"),
                            },
                            dataType: "json",
                            timeout: 5000,
                            success: function (data) {
                                if (data.success) {
                                    $.show_alert("成功", "节点数据保存成功！");
                                    //$("#ui_main_edit_dialog").dialog('destroy');保存后不自动关闭
                                    // $("#ui_main_dg").datagrid("reload");//.datagrid('clearSelections').datagrid('clearChecked');
                                } else {
                                    $.show_alert("提示", data.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                if (textStatus == "timeout") {
                                    $.show_alert("提示", "请求超时，请刷新当前页重试！");
                                }
                                else {
                                    $.show_alert("错误", textStatus + "：" + errorThrown);
                                }
                            }
                        });
                        lock = false;
                    }
                },
                {
                    text: '关闭',
                    iconCls: 'icon-cross',
                    handler: function () {
                        $("#ui_main_edit_dialog").dialog('destroy');
                        $("#ui_main_dg").datagrid("reload");
                    }
                }],
                //修改窗口加载后
                onLoad: function () {
                    bangdingnodedata(row);
                },
                //编辑窗口关闭
                onClose: function () {
                    $("#ui_main_edit_dialog").dialog('destroy');  //销毁dialog对象 
                    $("#ui_main_dg").datagrid("reload");//主表更新！
                }
            });

        }

        //查看项目
        function SeebillRDIndex() {
            var row = $("#ui_main_dg").datagrid("getSelected");
            if (row == null || row.length < 1) {
                $.show_alert("提示", "请先勾选要查看的项目");
                return;
            }
            if (row.length > 1) {
                $.show_alert("提示", "不支持批量查看项目");
                $("#ui_main_dg").datagrid('clearSelections').datagrid('clearChecked');
                return;
            }
            $("<div/>").dialog({
                id: "ui_main_view_dialog",
                href: "/RD/RDEdit",
                title: "查看项目",
                height: $(window).height(),
                width: $(window).width(),
                modal: true,
                toolbar: [
                {
                    text: '关闭',
                    iconCls: 'icon-cross',
                    handler: function () { $("#ui_main_view_dialog").dialog('destroy'); }
                }],
                //修改窗口加载后
                onLoad: function () {
                    bangdingnodedata(row);
                },
                onOpen: function () {
                    setTimeout(function () {
                        $(".easyui-textbox").textbox('readonly', true);//设置所有输入项为readonly
                        $(".easyui-datebox").textbox('readonly', true);//设置所有日期输入项为readonly
                        //$("#tt a.l-btn .icon-delete").parent().parent().linkbutton("disable");//禁止附件管理的删除、添加
                        $("#tt a.l-btn .icon-add").parent().parent().hide();
                        $("#tt a.l-btn .icon-delete").parent().parent().hide();
                    }, 2000);

                },
                //编辑窗口关闭
                onClose: function () {
                    $("#ui_main_view_dialog").dialog('destroy');  //销毁dialog对象                       
                }
            });
        }
        //项目信息视图绑定数据
        function bangdingnodedata(row) {
            //绑定项目基本信息
            $("#ProjectID").val(row.ProjectID);
            $("#ProjectNo").textbox().textbox('setValue', row.ProjectNo);
            $("#ProjectName").textbox().textbox('setValue', row.ProjectName);
            $("#ProjectManager").textbox().textbox('setValue', row.ProjectManager);
            $("#ProjectClerk").textbox().textbox('setValue', row.ProjectClerk);
            $('#startTime').datebox('setValue', row.startTime.replace(/\//g, '-'));
            $('#endTime').datebox('setValue', row.endTime.replace(/\//g, '-'));
            $('#RDType').combobox('setValue', row.RDType);

            $("#pLevel").textbox().textbox('setValue', row.pLevel);

            $('#aFactor').combobox('setValue', row.aFactor);
            $('#iFactor').combobox('setValue', row.iFactor);

            $("#RDEstCost").textbox().textbox('setValue', row.RDEstCost);
            $("#estPrice").textbox().textbox('setValue', row.estPrice);
            $("#FName").textbox().textbox('setValue', row.FName);  
            $('#Department').combobox('setValue', row.Department);
            //$("#com").hide();
            //$("#dep").hide();
            //$("#ProjectNo").textbox('enable');
            //$("#ProjectName").textbox('enable');
            //$("#ProjectManager").textbox('enable');
            //$("#ProjectClerk").textbox('enable');
            //$("#Remark").textbox('enable');

      
			var fileCols=[[  { field: 'AppendID', title: 'ID', sortable: false, hidden: true },
            {field: 'AppendPath', title: 'path', sortable: false, hidden: true},
                {field: 'AppendName', title: '文件名称', sortable: false, hidden: false},
                {field: 'UpdateTime', title: '上传时间', sortable: false, hidden: false},
                    {field: 'UpdateBy', title: '上传人', sortable: false, hidden: false},]];
            
            //加载当前项目的各节点时间进度信息
            $.ajax({
                url: "/RD/GetRDNodes?r=" + Math.random(),
                type: "post",
                data: { "ProjectID": row.ProjectID, "KeyName": "RDMaintain" },
                dataType: "json",
                timeout: 5000,
                success: function (data) {
                    if (data.success == true) {
                        //单据加载节点信息
                        for (var i = 0; i < data.rows.length; i++) {
                            //if (data.rows[i].NodeID != "11") continue;
                            //项目商务bs
                            if (data.rows[i].NodeID == "11") {
                                $("#bsExecutor").textbox('setValue', data.rows[i].executor);
                                $("#bsPSTime").datebox('setValue', data.rows[i].PSTime.replace(/\//g, '-'));//将全局的\号替换为-，使得js能识别为日期格式
                                $("#bsPETime").datebox('setValue', data.rows[i].PETime.replace(/\//g, '-'));                                
                                if (data.rows[i].status == 1) { $("#bsComplete").attr("checked", true); }
                                
                                $("#bsRemark").textbox().textbox('setValue', data.rows[i].Remark);
                                //加载附件,data.rows[i].AppendListID
                                var aid = data.rows[i].AppendListID;
                                $('#bsdatagrid').datagrid({
                                    url: "/Project/GetAppendList?AppendListID=" + aid + "&r=" + Math.random(),
                                    columns: fileCols, singleSelect: true,
                                    toolbar: [{
                                        text: '添加',
                                        iconCls: 'icon-add',
                                        handler: function () {
                                            uploadfile(aid, $("#bsdatagrid"));
                                        }
                                    }, {
                                        text: '删除',
                                        iconCls: 'icon-delete',
                                        handler: function () {
                                            Delfile($("#bsdatagrid"));
                                        }
                                    }, {
                                        text: '下载附件',
                                        iconCls: 'icon-save',
                                        handler: function () {
                                            downloadfile($("#bsdatagrid"));
                                        }
                                    }]
                                });
                            }
                                //合同签订ty
                            else if (data.rows[i].NodeID == "12") {
                                $("#tyExecutor").textbox('setValue', data.rows[i].executor);
                                $("#tyPSTime").datebox('setValue', data.rows[i].PSTime.replace(/\//g, '-'));
                                $("#tyPETime").datebox('setValue', data.rows[i].PETime.replace(/\//g, '-'));                               
                                $("#tyRemark").textbox().textbox('setValue', data.rows[i].Remark);
                                if (data.rows[i].status == 1) { $("#tyComplete").attr("checked", true); }
                                var aid3 = data.rows[i].AppendListID;
                                $('#tydatagrid').datagrid({
                                    url: "/Project/GetAppendList?AppendListID=" + aid3 + "&r=" + Math.random(),
                                    columns: fileCols, singleSelect: true,
                                    toolbar: [{
                                        text: '添加',
                                        iconCls: 'icon-add',
                                        handler: function () {
                                            uploadfile(aid3, $("#tydatagrid"));
                                        }
                                    }, {
                                        text: '删除',
                                        iconCls: 'icon-delete',
                                        handler: function () {
                                            Delfile($("#tydatagrid"));
                                        }
                                    }, {
                                        text: '下载附件',
                                        iconCls: 'icon-save',
                                        handler: function () {
                                            downloadfile($("#tydatagrid"));
                                        }
                                    }]
                                });
                            }
                                //设计时间dn
                            else if (data.rows[i].NodeID == "13") {                                
                                $("#dnExecutor").textbox('setValue', data.rows[i].executor);
                                $("#dnPSTime").datebox('setValue', data.rows[i].PSTime.replace(/\//g, '-'));
                                $("#dnPETime").datebox('setValue', data.rows[i].PETime.replace(/\//g, '-'));
                               
                                if (data.rows[i].status == 1) { $("#dnComplete").attr("checked", true); }
                                $("#dnRemark").textbox().textbox('setValue', data.rows[i].Remark);
                                var aid4 = data.rows[i].AppendListID;
                                $('#dndatagrid').datagrid({
                                    url: "/Project/GetAppendList?AppendListID=" + aid4 + "&r=" + Math.random(),
                                    columns: fileCols, singleSelect: true,
                                    toolbar: [{
                                        text: '添加',
                                        iconCls: 'icon-add',
                                        handler: function () {
                                            uploadfile(aid4, $("#dndatagrid"));
                                        }
                                    }, {
                                        text: '删除',
                                        iconCls: 'icon-delete',
                                        handler: function () {
                                            Delfile($("#dndatagrid"));
                                        }
                                    }, {
                                        text: '下载附件',
                                        iconCls: 'icon-save',
                                        handler: function () {
                                            downloadfile($("#dndatagrid"));
                                        }
                                    }]
                                });
                            }
                                //样机制作
                            else if (data.rows[i].NodeID == "14") {
                                $("#meExecutor").textbox().textbox('setValue', data.rows[i].executor);
                                $("#mePSTime").datebox('setValue', data.rows[i].PSTime.replace(/\//g, '-'));
                                $("#mePETime").datebox('setValue', data.rows[i].PETime.replace(/\//g, '-'));                                
                                $("#meRemark").textbox().textbox('setValue', data.rows[i].Remark);                              
                                if (data.rows[i].status == 1) { $("#meComplete").attr("checked", true); }
                                var aid5 = data.rows[i].AppendListID;
                                $('#medatagrid').datagrid({
                                    url: "/Project/GetAppendList?AppendListID=" + aid5 + "&r=" + Math.random(),
                                    columns: fileCols, singleSelect: true,
                                    toolbar: [{
                                        text: '添加',
                                        iconCls: 'icon-add',
                                        handler: function () {
                                            uploadfile(aid5, $("#medatagrid"));
                                        }
                                    }, {
                                        text: '删除',
                                        iconCls: 'icon-delete',
                                        handler: function () {
                                            Delfile($("#medatagrid"));
                                        }
                                    }, {
                                        text: '下载附件',
                                        iconCls: 'icon-save',
                                        handler: function () {
                                            downloadfile($("#medatagrid"));
                                        }
                                    }]
                                });

                            }
                                //施工管理cn
                            else if (data.rows[i].NodeID == "15") {
                                $("#cnExecutor").textbox().textbox('setValue', data.rows[i].executor);
                                $("#cnPSTime").datebox('setValue', data.rows[i].PSTime.replace(/\//g, '-'));
                                $("#cnPETime").datebox('setValue', data.rows[i].PETime.replace(/\//g, '-'));                           
                                $("#cnRemark").textbox().textbox('setValue', data.rows[i].Remark);
                                if (data.rows[i].status == 1) { $("#cnComplete").attr("checked", true); }
                                var aid6 = data.rows[i].AppendListID;
                                $('#cndatagrid').datagrid({
                                    url: "/Project/GetAppendList?AppendListID=" + data.rows[i].AppendListID + "&r=" + Math.random(),
                                    columns: fileCols, singleSelect: true,
                                    toolbar: [{
                                        text: '添加',
                                        iconCls: 'icon-add',
                                        handler: function () {
                                            uploadfile(aid6, $("#cndatagrid"));
                                        }
                                    }, {
                                        text: '删除',
                                        iconCls: 'icon-delete',
                                        handler: function () {
                                            Delfile($("#cndatagrid"));
                                        }
                                    }, {
                                        text: '下载附件',
                                        iconCls: 'icon-save',
                                        handler: function () {
                                            downloadfile($("#cndatagrid"));
                                        }
                                    }]
                                });
                            }
                        }

                    } else {
                        $.show_alert("错误", data.msg);
                    }
                }
                ,
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus == "timeout") {
                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                    }
                    else {
                        $.show_alert("错误", textStatus + "：" + errorThrown);
                    }
                }
            });

        }
    </script>
</head>
<body>
    <div id="ui_main_layout" class="easyui-layout" data-options="fit:true,border:false">
        <div data-options="region:'north',split:true,border:true,collapsed:true" title="条件搜索" style="height:104px;">
            <div id="ui_main_search">
                <table class="tableForm" style="width: 99%; height: 100%; background: #F5F5F5;">
                    <tr>
                        <th>
                            项目编码：
                        </th>
                        <td>
                            <input name="txtProjectNo" id="txtProjectNo" class="easyui-validatebox textbox" style="width:150px;height:22px;" />
                        </td>

                        <th>
                            项目名称：
                        </th>
                        <td>
                            <input name="txtRDName" id="txtRDName" class="easyui-validatebox textbox" style="width:150px;height:22px;" />
                        </td>

                        <th>
                            项目状态：
                        </th>
                        <td>
                            <select name="FStatus" id="FStatus">
                                <option value="select">请选择</option>
                                <option value="0">未确认</option>
                                <option value="1">已确认</option>
                                <option value="2">已完成</option>
                            </select>
                        </td>

                    <tr>
                        <th>
                            添加时间：
                        </th>
                        <td>
                            <input name="txtAddBeginDate" id="txtAddBeginDate" class="easyui-datetimebox"
                                   editable="false" style="width:150px;height:22px;" />
                        </td>

                        <th>
                            至：
                        </th>
                        <td>
                            <input name="txtAddEndDate" id="txtAddEndDate" class="easyui-datetimebox"
                                   editable="false" style="width:150px;height:22px;" />
                        </td>
                        <td colspan="4">
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                               onclick="ui_main_searchdata();">搜索</a>
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-clear" plain="true"
                               onclick="ui_main_cleardata();">清空条件</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div data-options="region:'center',border:false">
            <table id="ui_main_dg" data-options="fit:true,border:false"></table>
        </div>
    </div>
</body>
</html>
